﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlexCapture;
using System.Threading;

namespace MediaClient.Common
{
    public class PublishFile
    {
        private Thread transferThread;

        private double transferProcess;

        private long mediaPackID;

        private string videoFile = null;

        private List<string> audioFiles = null;

        public PublishFile()
        {
            
        }

        public delegate void TransferHandler(long mediaPackID,double process);

        public event TransferHandler TransferProcess;

        public ConvertHelper convertApp = new ConvertHelper();

        public void TransferThreadMethod()
        {
            convertApp.InitialFlexConvert(this.videoFile,this.audioFiles[0]);
            Thread.Sleep(1000);
            convertApp.StartFlexConvert();
            Thread.Sleep(200);
            try
            {
                while (transferProcess < 100)
                {
                    this.transferProcess = this.convertApp.GetProcess();
                    if (this.TransferProcess != null)
                        this.TransferProcess(this.mediaPackID, this.transferProcess);
                    Thread.Sleep(100);
                }
                this.transferThread.Suspend();
                this.transferThread = null;
            }
            catch (Exception myE)
            {
                throw myE;
            }
        }

        public void StartTransferFile(long mediaPackID, string videoFile, params string[] audioFile)
        {
            this.videoFile = videoFile;
            this.audioFiles = audioFile.ToList();
            this.mediaPackID = mediaPackID;
            
            this.transferThread = new Thread(TransferThreadMethod);
            this.transferThread.IsBackground = true;
            this.transferThread.Start();
        }
    }
}
